package com.lh.web.controller.notice;

import com.lh.common.entity.admin.Notice;
import com.lh.common.enums.IsDel;
import com.lh.common.enums.UpperLowerShelfStatus;
import com.lh.common.utils.PageUtils;
import com.lh.common.utils.Result;
import com.lh.web.controller.AbstractController;
import com.lh.web.service.serviceAdmin.notice.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author FH
 * @ClassName:
 * @Description: (公告管理)
 * @date: 2020-05-08 11:41
 */

@Api(value="NoticeAction",tags={"公告管理"})
@RestController
@RequestMapping("notice")
public class NoticeController extends AbstractController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 查询所有公告管理
     */
    @ApiOperation(value = "查询所有公告管理")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "status", value = "状态", required = false, dataTypeClass= UpperLowerShelfStatus.class)
    })
    @GetMapping("/getNoticeList")
    @RequiresPermissions("notice:getNoticeList")
    public Result<Notice>  getNoticeList(UpperLowerShelfStatus status, Integer pageNum, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        if(status != null) {
            params.put("status", status.getValue());
        }
        PageUtils pageUtils = noticeService.queryNoticeList(params, pageNum, pageSize);
        return Result.ok(pageUtils);
    }

    /**
     * 新增公告管理
     */
    @ApiOperation(value = "新增/修改公告管理")
    @PostMapping("/addNotice")
    @RequiresPermissions("notice:addNotice")
    public Result<Notice> addNotice(@RequestBody Notice notice) {
        try {
            notice.setUpdateTime(new Date());
            if(notice.getNid() == null) {
                notice.setCreateTime(new Date());
                notice.setStatus(UpperLowerShelfStatus.LOWERSHELF);
                notice.setIsDel(IsDel.NO);
                noticeService.insertNotice(notice);
                return Result.ok(notice);
            }
            noticeService.updateNotice(notice);
            return Result.ok();
        } catch (Exception e) {
            return Result.error();
        }
    }

    @ApiOperation(value = "notice上下架")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "nid", value = "nid", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query",name = "status", value = "状态", required = false, dataTypeClass = UpperLowerShelfStatus.class),
            @ApiImplicitParam(paramType="query",name = "isDel", value = "删除", required = false, dataTypeClass = IsDel.class)
    })
    @ResponseBody
    @GetMapping("/updateNoticeStatus")
    @RequiresPermissions("notice:updateNoticeStatus")
    public Result updateNoticeStatus(Long nid,UpperLowerShelfStatus status,IsDel isDel) {
        try {
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("nid",nid);
            if(status != null) {
                params.put("status",status.getValue());
            }
            if(isDel != null){
                params.put("isDel",isDel.getValue());
            }
            params.put("updateTime",new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
            noticeService.updateNoticeStatus(params);
            return Result.ok();
        } catch (Exception e) {
            return Result.error();
        }
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除公告")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "nid", value = "公告多个ID,逗号隔开", required = true)
    })
    @GetMapping("/deleteById")
    @ResponseBody
    @RequiresPermissions("notice:deleteById")
    public Result del(Long[] nid){
        try {
            noticeService.deleteNotice(nid);
            return Result.ok();
        } catch (Exception e) {
            return Result.error("删除公告异常!");
        }
    }
}
